//
//  WCDockBadgeView.m
//  WabbitCode
//
//  Created by William Towe on 11/14/08.
//  Copyright 2008 Revolution Software. All rights reserved.
//

#import "WCDockBadgeView.h"
#import "CTBadge.h"


@implementation WCDockBadgeView
@synthesize errorCount=_errorCount, warningCount=_warningCount;

+ (WCDockBadgeView *)sharedInstance; {
	static id _sharedInstance = nil;
	if (!_sharedInstance) {
		_sharedInstance = [[[self class] alloc] initWithFrame:[[[NSApp dockTile] contentView] frame]];
		[_sharedInstance setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
		[[NSApp dockTile] setContentView:_sharedInstance];
	}
	return _sharedInstance;
}

- (void)resetErrorAndWarningCount; {
	[self setErrorCount:0];
	[self setWarningCount:0];
	[[NSApp dockTile] display];
}

- (void)updateErrorCount:(NSUInteger)errorCount warningCount:(NSUInteger)warningCount; {
	[self setErrorCount:errorCount];
	[self setWarningCount:warningCount];
	[[NSApp dockTile] display];
}

- (void)dealloc {
	[_dockIcon release];
	[super dealloc];
}

- (void)drawRect:(NSRect)rect {
	if (!_dockIcon)
		_dockIcon = [[NSImage imageNamed:@"NSApplicationIcon"] copy];
	
	NSImage *dock = [_dockIcon copy];
	
	if ([self errorCount] && ![self warningCount]) {
		CTBadge *error = [CTBadge systemBadge];
		NSImage *eOverlay = [error badgeOverlayImageForValue:[self errorCount] insetX:0 y:0];
		
		[dock lockFocus];
		[eOverlay compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
		[dock unlockFocus];
	}
	else if (![self errorCount] && [self warningCount]) {
		CTBadge *warning = [CTBadge badgeWithColor:[NSColor yellowColor] labelColor:[NSColor textColor]];
		NSImage *wOverlay = [warning badgeOverlayImageForValue:[self warningCount] insetX:0 y:0];
		
		[dock lockFocus];
		[wOverlay compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
		[dock unlockFocus];
	}
	else if ([self errorCount] && [self warningCount]) {
		CTBadge *error = [CTBadge systemBadge];
		NSImage *eOverlay = [error badgeOverlayImageForValue:[self errorCount] insetX:0 y:0];
		CTBadge *warning = [CTBadge badgeWithColor:[NSColor yellowColor] labelColor:[NSColor textColor]];
		NSImage *wOverlay = [warning badgeOverlayImageForValue:[self warningCount] insetX:0 y:45];
		
		[dock lockFocus];
		[eOverlay compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
		[wOverlay compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
		[dock unlockFocus];
	}
	[dock compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
	[dock release];
}
@end
